﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK_HTTT_06.BUS;

namespace PTTK_HTTT_06.GUI.other
{
    public partial class MHThemMonAn : UserControl
    {
        int i = 0;
        public MHThemMonAn()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = dgvNguyenLieu.RowCount;
            i--;
            if (txtTenNCC.Text.ToString() != "" && txtSoLuong.Text.ToString() != "")
            {
                if (HopLe(dgvNguyenLieu, cmbNguyenLieu.Text.ToString(), i) == 1)
                {
                    DataGridViewRow _row = new DataGridViewRow();
                    dgvNguyenLieu.Rows.Add(_row);
                    dgvNguyenLieu.Rows[i].Cells[0].Value = cmbNguyenLieu.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[1].Value = txtSoLuong.Text.ToString();
                    dgvNguyenLieu.Rows[i].Cells[2].Value = cmbNguyenLieu.SelectedValue.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Nguyên liệu đã thêm bên dưới !");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin !");
                txtTenNCC.Focus();
            }
        }

        private int HopLe(DataGridView data, string str, int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (txtTenNCC.Text.ToString() != "")
            {
                MON_AN _monAn = new MON_AN();
                _monAn.TenMonAn = txtTenNCC.Text.ToString();
                MON_AN.ThemMonAn(_monAn);
                int _MaMonAn = _monAn.LayMaMonAnTheoTen(_monAn.TenMonAn);
                if (dgvNguyenLieu.Rows.Count > 1)
                {
                    CONG_THUC _CT = new CONG_THUC();
                    _CT.MaMonAn = _MaMonAn;
                    int i = 0;
                    while (dgvNguyenLieu.Rows.Count > 1)
                    {
                        if (dgvNguyenLieu.Rows[i].Cells[0].Value == null)
                        {
                            dgvNguyenLieu.Rows.RemoveAt(i);
                        }
                        else
                        {
                            _CT.MaNguyenLieu = int.Parse(dgvNguyenLieu.Rows[i].Cells[2].Value.ToString());
                            _CT.SoLuong = float.Parse(dgvNguyenLieu.Rows[i].Cells[2].Value.ToString());
                            CONG_THUC.ThemCongThuc(_CT);
                            dgvNguyenLieu.Rows.RemoveAt(i);
                        }
                    }
                    if (dgvNguyenLieu.RowCount == 1)
                    {
                        MessageBox.Show("Thêm thành công");
                    }
                }    
            }
            else
            {
                MessageBox.Show("Chưa nhập tên món ăn !");
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {            
            DialogResult _result = MessageBox.Show("Bạn có chắc muốn làm mới tất cả ?", "Xác nhận", MessageBoxButtons.OKCancel);
            if (_result == DialogResult.OK)
            {
                dgvNguyenLieu.Rows.Clear();
                txtTenNCC.Text = "";
                txtSoLuong.Text = "";
                txtTenNCC.Focus();
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

        private void MHThemMonAn_Load(object sender, EventArgs e)
        {            
            cmbNguyenLieu.DisplayMember = "TenNguyenLieu";
            cmbNguyenLieu.ValueMember = "MaNguyenLieu";
            cmbNguyenLieu.DataSource = NGUYEN_LIEU.LayDSTenNguyenLieu();
        }
    }
}
